export const imgZip = {
	data() {
		return {
			cWidth: 750,
			cHeight: 750
		}
	},
	methods: {
		img_zip(img) {
			let that = this
			return new Promise((resolve, reject) => {
				uni.getImageInfo({
					src: img,
					success: function(res) {
						var ratio = 2;
						var canvasWidth = res.width
						var canvasHeight = res.height
						while (canvasWidth > 300 || canvasHeight > 300) {
							canvasWidth = Math.trunc(res.width / ratio)
							canvasHeight = Math.trunc(res.height / ratio)
							ratio++;
						}

						that.cWidth = canvasWidth
						that.cHeight = canvasHeight

						var ctx = uni.createCanvasContext('canvas')
						ctx.drawImage(res.path, 0, 0, canvasWidth, canvasHeight)
						ctx.draw(false, setTimeout(function() {
							uni.canvasToTempFilePath({
								canvasId: 'canvas',
								destWidth: canvasWidth,
								destHeight: canvasHeight,
								success: function(res) {
									resolve(res.tempFilePath) //最终图片路径
								},
								fail: function(res) {
									reject(res.errMsg)
								}
							})
						}, 1000))
					}
				})
			})
		}
	}
}
